package com.kingtroldata.monitorview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 工艺监控显示界面
 * Created by ljb on 2017/2/16.
 */

public class MonitorView extends PhotoView {
    private static final String TAG = "MonitorView";
    private float design_width;//设置模板的宽
    private float design_height;//设计模板的高

    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Paint paint;

    public MonitorView(Context context) {
        this(context, null);
    }

    public MonitorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MonitorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false);
    }

    private int image_x;//图片x轴偏移量
    private int image_y;//图片y轴偏移量

    /**
     * 设置图片偏移量
     *
     * @param image_x x轴偏移量
     * @param image_y y轴偏移量
     */
    public void setImageTranslate(int image_x, int image_y) {
        this.image_x = image_x;
        this.image_y = image_y;
        Log.i(TAG, "setDesign: 设置图片的偏移量x轴:" + image_x + ";y轴:" + image_y);
    }


    private int text_x;//文本x轴偏移量
    private int text_y;//文本y轴偏移量

    /**
     * 设置文本偏移量
     *
     * @param text_x x轴偏移量
     * @param text_y y轴偏移量
     */
    public void setTextTranslate(int text_x, int text_y) {
        this.text_x = text_x;
        this.text_y = text_y;
        Log.i(TAG, "setDesign: 设置图片的偏移量x轴:" + text_x + ";y轴:" + text_y);
    }

    private int data_x;//数据x轴偏移量
    private int data_y;//数据y轴偏移量

    public void setDataTranslate(int data_x, int data_y) {
        this.data_x = data_x;
        this.data_y = data_y;
        Log.i(TAG, "setDesign: 设置数据的偏移量x轴:" + data_x + ";y轴:" + data_y);
    }

    /**
     * 设置模板尺寸
     *
     * @param design_width  设计的宽
     * @param design_height 设计的高
     */
    public void setDesign(float design_width, float design_height) {
        this.design_width = design_width;
        this.design_height = design_height;
        Log.i(TAG, "setDesign: 设置模板宽:" + design_width + ";高:" + design_height);
    }

    private Bitmap bg;//控件背景图片

    /**
     * 添加背景
     *
     * @param b Bitmap对象
     */
    public void addBackground(Bitmap b) {
        this.bg = b;
        invalidate();
    }

    /**
     * 添加ImageView
     *
     * @param b Bitmap对象
     * @param w 宽
     * @param h 高
     * @param x x坐标
     * @param y y坐标
     */
    public void addImageView(Bitmap b, int w, int h, int x, int y) {
        images.add(new ImageModel(b, w, h, x, y));
        invalidate();
    }

    /**
     * 添加TextView
     *
     * @param text 文本内容
     * @param size 文字大小
     * @param x    x轴坐标
     * @param y    y轴坐标
     */
    public void addTextView(String text, float size, int color, int x, int y) {
        texts.add(new TextModel(text, size, color, x, y));
        invalidate();
    }

    /**
     * 添加DataView
     *
     * @param data  数据
     * @param size  数据大小
     * @param color 颜色
     * @param x     x轴坐标
     * @param y     y轴坐标
     */
    public void addDataView(String data, float size, int color, int x, int y) {
        datas.add(new TextModel(data, size, color, x, y));
        invalidate();
    }

    public void cleanMonitorView(){
        setImageDrawable(null);
    }

    private int local_width;//实际控件的宽
    private int local_height;//实际控件的高

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w > h) {
            //获取控件的大小
            this.local_width = w;
            this.local_height = h;
            Log.i(TAG, "onSizeChanged: MonitorView控件的宽:" + w + ";高:" + h);

            initPaint();
        }
    }

    private void initPaint() {
        mBitmap = Bitmap.createBitmap(local_width, local_height, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        paint = new Paint();
        paint.setTypeface(Typeface.DEFAULT);//设置字体类型
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBg(mCanvas);
        drawImage(mCanvas);
        drawText(mCanvas);
        drawData(mCanvas);
    }

    //画数据
    private void drawData(Canvas canvas) {
        if (!datas.isEmpty()) {
            for (TextModel model : datas) {
                datas.remove(model);
                String text = model.text;
                float size = model.size;
                int color = model.color;
                paint.setTextSize(size);//设置字体大小
                paint.setColor(color);//设置画笔颜色
                int x = model.x + data_x;
                int y = model.y + data_y;
                int x_t = (int) (x * scale_x);
                int y_t = (int) (y * scale_y);
                drawData(canvas, text, x_t, y_t);
            }
        }
    }

    private int count_data;

    private void drawData(Canvas canvas, String text, int x_t, int y_t) {
        canvas.drawText(text, x_t, y_t, paint);
        setImageBitmap(mBitmap);
        Log.i(TAG, "drawImage: 添加了" + (++count_data) + "个Data");
    }

    //画文字
    private void drawText(Canvas canvas) {
        if (!texts.isEmpty()) {
            for (TextModel model : texts) {
                texts.remove(model);
                String text = model.text;
                float size = model.size;
                int color = model.color;
                paint.setTextSize(size);//设置字体大小
                paint.setColor(color);//设置画笔颜色
                int x = model.x + text_x;
                int y = model.y + text_y;
                int x_t = (int) (x * scale_x);
                int y_t = (int) (y * scale_y);
                drawText(canvas, text, x_t, y_t);
            }
        }
    }

    private int count_text;

    private void drawText(Canvas canvas, String text, int x_t, int y_t) {
        canvas.drawText(text, x_t, y_t, paint);
        setImageBitmap(mBitmap);
        Log.i(TAG, "drawImage: 添加了" + (++count_text) + "个TextView");
    }

    //画图片
    private void drawImage(Canvas canvas) {
        if (!images.isEmpty()) {
            for (ImageModel model : images) {
                images.remove(model);
                Bitmap b = model.b;
                int w = model.w;
                int h = model.h;
                int x = model.x + image_x;
                int y = model.y + image_y;
                int w_t = (int) (w * scale_x);
                int h_t = (int) (h * scale_y);
                int x_t = (int) (x * scale_x);
                int y_t = (int) (y * scale_y);
                drawImage(canvas, b, w_t, h_t, x_t, y_t);
            }
        }
    }

    int count_image;

    private void drawImage(Canvas canvas, Bitmap b, int w, int h, int x, int y) {
        Rect srcRect = new Rect(0, 0, b.getWidth(), b.getHeight());
        Rect dstRect = new Rect(x, y, x + w, y + h);
        canvas.drawBitmap(b, srcRect, dstRect, null);
        setImageBitmap(mBitmap);
        Log.i(TAG, "drawImage: 添加了" + (++count_image) + "个ImageView");
    }

    //画背景
    private void drawBg(Canvas canvas) {
        if (bg != null) {
            getViewScale();
            Rect srcRect = new Rect(0, 0, bg.getWidth(), bg.getHeight());
            Rect dstRect = new Rect(0, 0, local_width, local_height);
            canvas.drawBitmap(bg, srcRect, dstRect, null);
            setImageBitmap(mBitmap);
            Log.i(TAG, "drawImage: 添加背景图片");
            bg = null;
        }
    }

    private float scale_x;//y轴缩放比
    private float scale_y;//x轴缩放比

    //获取x轴,y轴的缩放比例
    private void getViewScale() {
        scale_x = local_width / design_width;
        scale_y = local_height / design_height;
        Log.i(TAG, "getViewScale: x轴缩放比:" + scale_x + ";y轴缩放比:" + scale_y);
    }

    private CopyOnWriteArrayList<ImageModel> images = new CopyOnWriteArrayList<>();//ImageView数据集合
    private CopyOnWriteArrayList<TextModel> texts = new CopyOnWriteArrayList<>();//TextView数据集合
    private CopyOnWriteArrayList<TextModel> datas = new CopyOnWriteArrayList<>();//DataView数据集合

    //ImageView数据模型
    static class ImageModel {
        Bitmap b;
        int w, h, x, y;

        ImageModel(Bitmap b, int w, int h, int x, int y) {
            this.b = b;
            this.w = w;
            this.h = h;
            this.x = x;
            this.y = y;
        }
    }

    //TextView数据模型
    static class TextModel {
        String text;
        float size;
        int color, x, y;

        TextModel(String text, float size, int color, int x, int y) {
            this.text = text;
            this.size = size;
            this.color = color;
            this.x = x;
            this.y = y;
        }
    }
}
